// import { MessageBoxService } from './../../components/messageBox/messageBoxService';
// import { GrowlMessageType } from './../../components/messageBox/growMeaageType';
import { debounce } from "../utils/extended";
import * as R from "ramda";
import { CoreMessageProvider } from "src/common/core/providers/message";

export function OperationLock() {
    let pass = true;
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        const method = descriptor.value;
        descriptor.value = async function (...args:any[]) {
            if (pass) {
                pass = false;
            } else {
                return;
            }
            try {
                const that = this;
                const result = await method.apply(that, args);
                return result;
            } catch (error) {
                throw error;
            } finally {
                pass = true;
            }
        }
    }
}


export function CatchExpection() {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        const method = descriptor.value;
        descriptor.value = async function (...args:any[]) {
            try {
                const that = this;
                const result = await method.apply(that, args);
                return result;
            } catch (error) {
                CoreMessageProvider.current.error(error.message)
            }
        }
    }
}

export function Search(wait:number) {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        const method = descriptor.value;
        descriptor.value = debounce(method,wait,false)
    }
}

export function HandleOperation() {
    let canPass = true;
    const argsCatch:any[] = [];
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        // descriptor.value = debounce(method,20,false)
        const method = descriptor.value
        async function newMethod(...args:any[]) {
            try {
                if (!canPass) {
                    return
                }
                canPass = false
                const that = this;
                const result = await method.apply(that, argsCatch)
                return result;
            } catch (error) {
                CoreMessageProvider.current.error(error.message)
            } finally {
                canPass = true;
            }
        }
        const debounceMethod = debounce(newMethod,20,false)
        descriptor.value = async function (...args:any[]) {
            // todo array merge
            console.log(argsCatch, args)
            argsCatch[0] = R.merge(argsCatch[0], args[0])
            debounceMethod.bind(this)(...argsCatch)
        }
    }
}

export function Debounce(wait:number) {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        const method = descriptor.value;
        descriptor.value = debounce(method,wait,false)
    }
}

export function Once() {
    return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
        const method = descriptor.value;
        descriptor.value = R.once(method)
    }
}